import { baseApiUrl, path } from "../../utils/config";
import { address, addressDefault, addressDel } from '../../utils/api';
const app = getApp();
import Toast from '@vant/weapp/toast/toast';
Page({
  data: {
    baseImageUrl: baseApiUrl,
    is: false,

    list: []
  },
  onLoad(options) {
    let { is } = options;
    this.setData({ is });
  },
  onShow() { this.getList() },
  async getList() {
    let res = await address();
    let list = res.address;
    this.setData({ list });
  },
  async setup_checked(e) {
    let { id } = e.currentTarget.dataset;
    await addressDefault({ id });
    Toast.success('操作成功');
    this.getList();
  },
  async delete_item(e) {
    let { id, index } = e.currentTarget.dataset;
    await addressDel({ id });
    Toast.success('操作成功');
    setTimeout(() => {
      let list = this.data.list;
      list.splice(index, 1);
      this.setData({ list });
    }, 1000);
  },
  router(e) {
    let { url, id } = e.currentTarget.dataset;
    url = id ? `${url}?id=${id}` : url;
    wx.navigateTo({ url });
  },
  checked_address(e) {
    let is = this.data.is;
    if (!is) return false;
    let { item } = e.currentTarget.dataset;
    app.globalData.address = item;
    wx.navigateBack();
  },
  onShareAppMessage() {
    let poster = app.globalData.poster;
    return {
      path,
      imageUrl: poster
    }
  },
  onShareTimeline() { }
})